var storeLoaded = false;
function storeLoad() {
	var $page = $("#strLst"),

	// Get the header for the page.
	// $header = $page.children(":jqmData(role=header)"),

	// Get the content area element for the page.
	$content = $page.children(":jqmData(role=content)");
	if (!storeLoaded) {
		var items = [];
		$.each(stores, function(key, val) {
			if (!prodCnt[val.id])
				prodCnt[val.id] = 0;
			// if (prodCnt[val.nm]) {
			items.push('<li id="' + key + '"><a href="#prodLst?s=' + val.id
					+ '">' + val.nm + '<span class="ui-li-count">'
					+ prodCnt[val.id] + '</span></a></li>');
			// }

		});

		$('<ul/>', {
			'data-autodividers' : 'true',
			'data-role' : 'listview',
			'data-inset' : 'true',
			'class' : 'my-new-list',
			"data-filter" : "true",
			html : items.join('')
		}).appendTo($content);

		$page.page();
		$content.find(":jqmData(role=listview)").listview();
		storeLoaded = true;
	}
	$.mobile.changePage('#strLst');
}

var linkParams = [];

// Listen for any attempts to call changePage().
$(document).bind(
		"pagebeforechange",
		function(e, data) {

			if (typeof data.toPage === "string") {

				if (!data.options.reverse)
					linkParams[$.mobile.urlHistory.activeIndex] = data.absUrl
							.substr(data.absUrl.indexOf("#"));

				var u = $.mobile.path.parseUrl(data.toPage), re;

				re = /^#prodLst/;
				if (u.hash.search(re) !== -1) {
					showProducts(u, data.options);
					e.preventDefault();
				}

				re = /^#prodDet/;
				if (u.hash.search(re) !== -1) {
					showProductDet(u, data.options);
					e.preventDefault();
				}

				re = /^#catLst/;
				if (u.hash.search(re) !== -1) {
					showCatLst(u, data.options);
					e.preventDefault();
				}

			}
		});

function showCatLst(urlObj, options) {

	var url = urlObj.hash;
	if (options.direction === "back")
		url = linkParams[$.mobile.urlHistory.activeIndex - 1];

	var rule = url.replace(/.*\?/, ""), pageSelector = url.replace(/\?.*$/, "");
	var pn = rule, pv = "", tit = "Kategorien";
	if (rule.indexOf("=") > 0) {
		pn = rule.split("=")[0];
		pv = rule.split("=")[1];

		tit += " in " + pv;
	}

	// alert(pageSelector);

	var $page = $(pageSelector),

	// Get the header for the page.
	$header = $page.children(":jqmData(role=header)"),

	// Get the content area element for the page.
	$content = $page.children(":jqmData(role=content)"), markup = "<ul data-role='listview' data-inset='true' data-filter='true' data-autodividers='true'>";

	for ( var i = 0; i < cats.length; i++) {
		if (pn === 's' && cats[i].vt !== pv)
			continue;
		if (prodCnt[cats[i].id])	
		markup += '<li><a href="#prodLst?c=' + cats[i].id + '">' + cats[i].nm
				+ '<span class="ui-li-count">'
					+ prodCnt[cats[i].id] + '</span></a></li>';
	}

	markup += "</ul>";
	$header.find("h1").html(tit);
	$content.html(markup);
	$page.page();
	$content.find(":jqmData(role=listview)").listview();
	options.dataUrl = urlObj.href;
	$.mobile.changePage($page, options);
}

function showProducts(urlObj, options, url) {

	var url = urlObj.hash;
	if (options.direction === "back")
		url = linkParams[$.mobile.urlHistory.activeIndex - 1];

	var rule = url.replace(/.*\?/, ""), pageSelector = url.replace(/\?.*$/, "");
	var pn = rule, pv = "", tit = "Artikel";
	if (rule.indexOf("=") > 0) {
		pn = rule.split("=")[0];
		pv = rule.split("=")[1];

		tit += " in " + pv;
	}

	// alert(pageSelector);

	var $page = $(pageSelector),

	// Get the header for the page.
	$header = $page.children(":jqmData(role=header)"),

	// Get the content area element for the page.
	$content = $page.children(":jqmData(role=content)"), markup = "<ul data-role='listview' data-inset='true' data-filter='true' data-autodividers='true'>";

	for ( var i = 0; i < prod.length; i++) {
		if (pn === 's' && prod[i].vt !== pv)
			continue;
		if (pn === 'c' && prod[i].ct !== pv)
			continue;
		markup += '<li><a href="#prodDet?prw=' + i + '">' + prod[i].nm
				+ "</a></li>";
	}

	markup += "</ul>";
	$header.find("h1").html(tit);
	$content.html(markup);
	$page.page();
	$content.find(":jqmData(role=listview)").listview();
	options.dataUrl = urlObj.href;
	$.mobile.changePage($page, options);
}
